package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {


    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private CheckItemDao checkItemDao;

    //添加t_checkgroup表数据
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkItemIds) {

        //group表添加数据
        checkGroupDao.add(checkGroup);

        setCheckGroupAndCheckItem(checkGroup.getId(), checkItemIds);

    }


    //添加中间表的数据
    private void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkItemIds) {

        if (checkItemIds != null && checkItemIds.length > 0) {
            for (Integer checkItemId : checkItemIds) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("checkgroup_id", checkGroupId);
                map.put("checkitem_id", checkItemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }

        }

    }


    //查询分页数据
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        PageHelper.startPage(currentPage, pageSize);

        Page<CheckGroup> page = checkGroupDao.findPage(queryString);

        long total = page.getTotal();
        List<CheckGroup> rows = page.getResult();
        return new PageResult(total, rows);
    }

    //根据id删除数据
    @Override
    public void delete(Integer id) {

        //操作级联删除操作

        //根据checkgroup_id删除t_checkgroup_checktime中的数据
        checkGroupDao.deleteBycheckgroup_id(id);
        //根据id删除t_checkgroup中的数据
        checkGroupDao.deleteById(id);

    }

    //根据id查询check表
    @Override
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup = checkGroupDao.findById(id);

        checkGroup.setCheckItems(checkItemDao.findAll());
        return checkGroup;
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }


    //修改检查组的
    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {

        //先修改checkgroup表的数据
        checkGroupDao.edit(checkGroup);
        //根据checkgroup_id删除中间表
        checkGroupDao.deleteBycheckgroup_id(checkGroup.getId());

        //添加t_checkgroup和t_checkgroup_checkitem表建立关系
        setCheckGroupAndCheckItem(checkGroup.getId(), checkitemIds);
    }

    //查询所有检查组
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }


}
